package com.cleaningcloud.erp.utils;

import com.cleaningcloud.common.core.utils.UniqueIdUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import com.cleaningcloud.erp.domain.*;
import com.cleaningcloud.erp.domain.vo.ErpOutboundDetailsVo;
import com.cleaningcloud.erp.domain.vo.ErpProcureDetailsVo;
import com.cleaningcloud.erp.domain.vo.ErpReturnDetailsVo;
import com.cleaningcloud.erp.domain.vo.ErpSaleDetailsVo;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

public class ProcureSaleUtils {

    /**
     * 销售单字段赋值
     *
     * @param erpAccountsReceivable
     * @param wareId
     * @param answerScheduledReceipt
     * @return
     */
    public static ErpOutbound setOutboundFields(ErpAccountsReceivable erpAccountsReceivable, Long wareId, int answerScheduledReceipt) {
        ErpOutbound erpOutbound = new ErpOutbound();
        erpOutbound.setAnswerScheduledReceipt(answerScheduledReceipt);
        erpOutbound.setWareId(wareId);
        erpOutbound.setReceivableId(erpAccountsReceivable.getReceivableId());
        erpOutbound.setOutboundNo("CK" + UniqueIdUtils.generateUniqueId());
        erpOutbound.setOutboundStatus(1);
        erpOutbound.setCreateId(SecurityUtils.getUserId());
        erpOutbound.setCreateName(SecurityUtils.getUsername());
        erpOutbound.setCreateTime(new Date());
        erpOutbound.setFlag("Y");
        erpOutbound.setOutboundType(erpAccountsReceivable.getOddType());
        if (erpAccountsReceivable.getOddType() == 3) {
            erpOutbound.setRemark("销售退换货");
        }
        if (erpAccountsReceivable.getOddType() == 2) {
            erpOutbound.setRemark("采购退换货");
        }
        return erpOutbound;
    }

    /**
     * 采购单入库字段赋值
     *
     * @param erpAccountsReceivable
     * @param wareId
     * @param answerScheduledReceipt
     * @return
     */
    public static ErpWarehousing setWarehousingFields(ErpAccountsPayable erpAccountsPayable, Long wareId, int answerScheduledReceipt) {
        ErpWarehousing erpWarehousing = new ErpWarehousing();
        erpWarehousing.setAnswerScheduledReceipt(answerScheduledReceipt);
        erpWarehousing.setWareId(wareId);
        erpWarehousing.setPayableId(erpAccountsPayable.getPayableId());
        erpWarehousing.setWarehousingType(erpAccountsPayable.getOddType());
        erpWarehousing.setCreateId(SecurityUtils.getUserId());
        erpWarehousing.setCreateName(SecurityUtils.getUsername());
        erpWarehousing.setCreateTime(new Date());
        erpWarehousing.setWarehousingNo("RK" + UniqueIdUtils.generateUniqueId());
        erpWarehousing.setWarehousingStatus(1);
        erpWarehousing.setFlag("Y");
        if (erpAccountsPayable.getOddType() == 3) {
            erpWarehousing.setRemark("采购退换货");
        }
        if (erpAccountsPayable.getOddType() == 2) {
            erpWarehousing.setRemark("销售退换货");
        }
        return erpWarehousing;
    }

    /**
     * 根据销售单生成销售出库明细
     *
     * @param list
     * @param erpOutbound
     * @return
     */
    public static List<ErpOutboundDetails> getErpOutboundDetails(List<ErpSaleDetailsVo> list, ErpOutbound erpOutbound) {
        return list
                .stream()
                .map(
                        obj -> {
                            ErpOutboundDetails erpOutboundDetails = new ErpOutboundDetailsVo();
                            erpOutboundDetails.setOutboundId(erpOutbound.getOutboundId());
                            erpOutboundDetails.setWareId(obj.getOutWareId());
                            erpOutboundDetails.setOutboundOdd(erpOutbound.getOutboundNo());
                            erpOutboundDetails.setCommodityId(obj.getCommodityId());
                            erpOutboundDetails.setPendingNumber(null == obj.getUnitResult() ? 1 : Integer.valueOf(obj.getUnitResult()) * obj.getSaleNumber());
                            erpOutboundDetails.setSaleNumber(null == obj.getUnitResult() ? 1 : Integer.valueOf(obj.getUnitResult()) * obj.getSaleNumber());
                            return erpOutboundDetails;
                        }
                ).collect(Collectors.toList());
    }

    /**
     * 根据采购/销售退货单生成销售出库明细
     *
     * @param list
     * @param erpOutbound
     * @return
     */
    public static List<ErpOutboundDetails> getErpOutboundDetailsByProcudeReturn(List<ErpReturnDetailsVo> list, ErpOutbound erpOutbound) {
        return list
                .stream()
                .map(
                        obj -> {
                            ErpOutboundDetails erpOutboundDetails = new ErpOutboundDetailsVo();
                            erpOutboundDetails.setOutboundId(erpOutbound.getOutboundId());
                            erpOutboundDetails.setWareId(erpOutbound.getWareId());
                            erpOutboundDetails.setOutboundOdd(erpOutbound.getOutboundNo());
                            erpOutboundDetails.setCommodityId(obj.getCommodityId());
                            erpOutboundDetails.setPendingNumber(Integer.valueOf(obj.getUnitResult()) * obj.getCommodityNumber());
                            erpOutboundDetails.setSaleNumber(Integer.valueOf(obj.getUnitResult()) * obj.getCommodityNumber());
                            return erpOutboundDetails;
                        }
                ).collect(Collectors.toList());
    }

    /**
     * 根据采购/销售退货单生成采购入库明细
     *
     * @param list
     * @param erpWarehousing
     * @return
     */
    public static List<ErpWarehousingDetails> getErpWarehousingDetailsByProcudeReturn(List<ErpReturnDetailsVo> list, ErpWarehousing erpWarehousing) {
        return list
                .stream()
                .map(
                        obj -> {
                            ErpWarehousingDetails erpWarehousingDetails = new ErpWarehousingDetails();
                            erpWarehousingDetails.setWarehousingId(erpWarehousing.getWarehousingId());
                            erpWarehousingDetails.setWarehousingOdd(erpWarehousing.getWarehousingNo());
                            erpWarehousingDetails.setCommodityId(obj.getCommodityId());
                            erpWarehousingDetails.setPendingNumber(Integer.valueOf(obj.getUnitResult()) * obj.getCommodityNumber());
                            erpWarehousingDetails.setProcureNumber(Integer.valueOf(obj.getUnitResult()) * obj.getCommodityNumber());
                            return erpWarehousingDetails;
                        }
                ).collect(Collectors.toList());
    }

    /**
     * 根据采购单生成采购入库明细
     *
     * @param list
     * @param erpOutbound
     * @return
     */
    public List<ErpWarehousingDetails> getErpWarehousingDetails(List<ErpProcureDetailsVo> list, ErpWarehousing erpWarehousing) {
        return list
                .stream()
                .map(
                        obj -> {
                            ErpWarehousingDetails warehousingDetails = new ErpWarehousingDetails();
                            warehousingDetails.setWarehousingId(erpWarehousing.getWarehousingId());
                            warehousingDetails.setWarehousingOdd(erpWarehousing.getWarehousingNo());
                            warehousingDetails.setCommodityId(obj.getCommodityKey());
                            warehousingDetails.setPendingNumber(obj.getProcureNumber());
                            warehousingDetails.setProcureNumber(obj.getProcureNumber());
                            return warehousingDetails;
                        }
                ).collect(Collectors.toList());
    }
}
